//
//  GifController.m
//  Mango
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "GifController.h"
#import "MJRefresh.h"
#import "BaseModel.h"
#import "GifCell.h"
#import "BaseCell.h"
#import "UMSocial.h"
#define Gif_Url @"http://api.app.happyjuzi.com/article/list/gif"

@interface GifController ()<GifCellDelegate>
@property (nonatomic, strong) NSMutableArray *pictureArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation GifController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pictureArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"GifCell" bundle:nil] forCellReuseIdentifier:@"GifModel"];
    self.tableView.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:0.7];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self refreshData];
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    } else {//日间模式
    }
    [self.tableView reloadData];
}



//添加刷新
- (void)refreshData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(LoadMoreData)];
}
//获取新数据
- (void)loadNewData {
    self.page = 1;
    [self loadDataWithPage:self.page];
}
//加载更多数据
- (void)LoadMoreData {
    self.page++;
    [self loadDataWithPage:self.page];
}
//请求网络数据
- (void)loadDataWithPage:(NSInteger)page {
    NSDictionary *dict = @{@"net":@"wifi",@"page":@(page),@"pf":@"ios",@"res":@"320x568",@"size":@"20",@"uid":@"3971854834692451",@"ver":@"2.11"};
   
    [[AFHTTPSessionManager manager]GET:Gif_Url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDict = responseObject[@"data"];
        for (NSDictionary *itemDict in dataDict[@"list"]) {
            BaseModel *model = [BaseModel baseModelWithDictionary:itemDict];
            [self.pictureArray addObject:model];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.pictureArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BaseCell *cell = [BaseCell cellWithTableView:tableView andIndexPath:indexPath andModel:self.pictureArray[indexPath.row]];
    GifCell *gifCell = (GifCell *)cell;
    gifCell.indexPath = indexPath;
    gifCell.delegate = self;
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [gifCell updateToNightSkinMode];
    }else {
        [gifCell updateToDaySkinMode];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return gifCell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GifModel *model = self.pictureArray[indexPath.row];
    return model.cellHeight;
}

#pragma mark - gifCellDelegate
- (void)gifCellDidClickGifBtn:(NSIndexPath *)indexPath {
    if (self.indexPath == indexPath) {
        return;
    }
    GifCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
    [cell resetImageView];
    self.indexPath = indexPath;
}

- (void)gifCellDidClickShareBtn:(GifModel *)model {
    //构造分享内容
    NSString *imgUrlStr = [model.pic componentsSeparatedByString:@"!"][0];
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:imgUrlStr];
    //    [UMSocialData defaultData].extConfig.title = model.title;
    [UMSocialData defaultData].shareText = model.title;
    [UMSocialData defaultData].shareImage = imgUrlStr;
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:@"sina"];
    snsPlatform.snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (![self.tableView.indexPathsForVisibleRows containsObject:self.indexPath]) {
        GifCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
        [cell resetImageView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.view.window == nil && [self isViewLoaded]) {
        self.view = nil;
    }
}
@end
